<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title></title>
  <th:block th:include="common/pub_head :: default"></th:block>
  <th:block th:include="common/pub_head :: layui"></th:block>
</head>
<body class="container" style="padding: 4px 5px">
<form class="layui-form layui-form-pane" id="list_form">
  <input type="hidden" name="id" id="id" th:value="${storeBean.id}">
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">门店名称<span class="f-red">*</span></label>
      <div class="layui-input-block">
        <input type="text" lay-verify="required|repeatName" placeholder="请输入门店名称"
               name="name" id="name"
               th:value="${storeBean.name}"
               autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-inline">
      <label class="layui-form-label">排序<span class="f-red">*</span></label>
      <div class="layui-input-block">
        <input type="text" lay-verify="required" placeholder="请输入排序"
               name="orderNum" id="orderNum"
               onkeyup="limitDigit(this)" onblur="limitDigit(this)"
               th:value="${storeBean.orderNum}"
               autocomplete="off" class="layui-input">
      </div>
    </div>
  </div>
  <div class="layui-form-item" style="text-align: right">
    <button type="button" lay-submit="" lay-filter="save" class="layui-btn">保存</button>
    <button type="button" class="layui-btn layui-btn-primary" onclick="resetForm()">重置</button>
  </div>
</form>
<script>
  /*<![CDATA[*/
  var form;
  layui.use(['form'], function () {
    form = layui.form;

    form.verify({
      repeatName: function (value) {
        var formData = {
          value: value,
          tableName: "cm_store",
          col: "name",
          primaryKey: "id",
          primaryKeyValue: $("#id").val()
        };
        var msg = "门店名称已经存在";
        return repeat(formData, msg);
      }
    });

    form.on('submit(save)', function (data) {
      saveAndFlushStore();
    });
  });

  function saveAndFlushStore() {
    var URL = path + "/store/saveAndFlushStore";
    var formData = $("#list_form").serializeArray();

    var load = layer.load();
    $.post(URL, formData, function (jsonData) {
      if (jsonData.flag) {
        layer.msg("保存成功", {icon: 1});
        setTimeout(function () {
          window.parent.listStoreByParams();
        }, 1000);
      } else {
        layer.msg("保存失败", {icon: 2});
        layer.close(load);
      }
    }, "json")
  }

  function resetForm() {
    $("#list_form")[0].reset();
  }

  /*]]>*/
</script>
</body>
</html>